package com.youlu.campus.admin.order;

import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.VO.req.OrderPriceStatisticsQueryVO;
import com.youlu.campus.entity.data.OrderPriceStatistics;
import com.youlu.campus.service.order.OrderPriceStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;

@RestController
public class OrderPriceStatisticsController {
    @Autowired
    private OrderPriceStatisticsService orderPriceStatisticsService;

    /**
     * 订单价格统计
     *
     * @param req
     * @return
     */
    @PostMapping("/order-statistics/price/list")
    public RestResult list(@RequestBody @Valid OrderPriceStatisticsQueryVO req) {
        String comId = AuthUtils.getComId();
        Page<OrderPriceStatistics> datas = orderPriceStatisticsService.list(req);
        if (datas == null) {
            PageRequest pageRequest = PageRequest.of(req.getPage() - 1, req.getPageSize());
            datas = PageableExecutionUtils.getPage(new ArrayList<>(), pageRequest, () -> 0L);
        }
        return RestResultBuilder.builder().success().data(datas).build();
    }

    @PostMapping("/order-statistics/price/data")
    public RestResult data(@RequestBody OrderPriceStatisticsQueryVO req) {
        String comId = AuthUtils.getComId();
        orderPriceStatisticsService.all(req);
        return RestResultBuilder.builder().success().data("ok").build();
    }
}
